package com.summer.mall.controller.admin;


import com.summer.mall.common.Const;
import com.summer.mall.common.ResponseCode;
import com.summer.mall.common.ResponseService;
import com.summer.mall.entity.User;
import com.summer.mall.service.CategoryService;
import com.summer.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private HttpSession httpSession;

    /**
     * 添加节点接口
     * @param categoryName
     * @param parentId
     * @param session
     * @return
     */
    @GetMapping(value = "/add_category")
    @ResponseBody
    public ResponseService addCategory(String categoryName, @RequestParam(value="parentId",defaultValue = "0") Integer parentId, HttpSession session){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return  ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登陆");
        }else{
            //校验是不是管理员用户
            ResponseService<String> response = userService.checkAdminRole(currentUser);
            if (!response.isSuccess()){
                return response;
            }
        }
        return categoryService.addCategory(categoryName,parentId);
    }

    /**
     * 修改节点名称接口
     * @param categoryName
     * @param categoryId
     * @param session
     * @return
     */
    @GetMapping(value = "/set_category_name")
    @ResponseBody
    public ResponseService setCategoryName(String categoryName, Integer categoryId,HttpSession session){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return  ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登陆");
        }else{
            //校验是不是管理员用户
            if (!userService.checkAdminRole(currentUser).isSuccess()){
                return userService.checkAdminRole(currentUser);
            }
        }
        return categoryService.updateCategory(categoryName,categoryId);
    }

    /**
     * 获取平级子节点接口
     * @param parentId
     * @param session
     * @return
     */
    @GetMapping(value = "/get_category")
    @ResponseBody
    public ResponseService getChildrenParallelCategory(@RequestParam(value = "parentId",defaultValue = "0") Integer parentId,HttpSession session){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return  ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登陆");
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()){
            return categoryService.getChildrenParallelCategory(parentId);
        }else{
            return userService.checkAdminRole(currentUser);
        }
    }

    /**
     * 查找该商品分类Id以及其子Id接口
     * @param categoryId
     * @param session
     * @return
     */
    @GetMapping(value = "/get_deep_category")
    @ResponseBody
    public ResponseService selectCategoryAndChildrenById(Integer categoryId,HttpSession session){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return  ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登陆");
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()){
            return categoryService.selectCategoryAndChildrenById(categoryId);
        }else{
            return userService.checkAdminRole(currentUser);
        }
    }

    /**
     * 获取指定节点信息接口
     * @param session
     * @param id
     * @return
     */
    @GetMapping(value = "/get_by_id")
    @ResponseBody
    public ResponseService getById(HttpSession session,Integer id){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if(id != null)
            return categoryService.getById(id);
        else
            return ResponseService.createByErrorMessage("当前输入的id为空");
    }

}
